﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using SwUI.PO;
using System.Windows;

namespace SwUI.XML
{
    public class XmlUtil
    {
        private static XmlUtil xmlObj = null;
        private XmlDocument doc = null;

        //不允许使用new 来生成
        private XmlUtil(){}

        private XmlUtil(string xmlPath)
        {
            doc = new XmlDocument();
            doc.Load(xmlPath);
        }
        /// <summary>
        /// 该方法用于获取读取XML对象
        /// 
        /// 调用该方法获得XmlUtil对象
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static XmlUtil GetXmlObj(string xmlPath)
        {
            XmlUtil obj = null; 
            if (File.Exists(xmlPath))
            {
                obj = new XmlUtil(xmlPath);
            }
            xmlObj = obj;
            return obj;
        }
        /// <summary>
        /// 该方法用于获取创建XML对象
        /// 
        /// 调用该方法创建XML文件并获取XmlUtil对象
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static XmlUtil CreateXmlObj(string xmlPath)
        {
            if (File.Exists(xmlPath))
            {
                MessageBox.Show("文件已存在，请使用其他文件名!");
                return null;
            }
            else
            {
                XmlDocument t_doc = new XmlDocument();

                XmlDeclaration dec = t_doc.CreateXmlDeclaration("1.0", "utf-8", null);
                t_doc.AppendChild(dec);
                XmlElement rootElement = t_doc.CreateElement("分类");
                t_doc.AppendChild(rootElement);
                t_doc.Save(xmlPath);
            }
            XmlUtil obj = GetXmlObj(xmlPath);
            return obj;
        }
        public void Dispose()
        {
            
        }

        [Obsolete("该方法未使用")]
        public PartNodeInfo GetSpecifyChildNode(PartNodeInfo nodeInfo)
        {
            PartNodeInfo pInfo = new PartNodeInfo();
            XmlNode node = doc.SelectSingleNode(nodeInfo.nodePath);
            if (node != null)
            {
                GetSingleNodeAttributes(node, pInfo);
            }
            return pInfo;
        }

        private void GetSingleNodeAttributes(XmlNode t_node1, PartNodeInfo pInfo)
        {
            pInfo.nodeName = t_node1.Name;
            pInfo.nodePath = pInfo.pNodePath + "/" + t_node1.Name;
            XmlAttributeCollection xCol = t_node1.Attributes;
            foreach (XmlAttribute attribute in xCol)
            {
                string attributeName = attribute.Name;
                string attributeValue = attribute.Value;
                switch (attributeName)
                {
                    case "name": pInfo.name = attributeValue; break;
                    case "ID": pInfo.ID = attributeValue; break;
                    case "设计规范": pInfo.stdDesign = attributeValue; break;
                    case "模板图纸": pInfo.temDraw = attributeValue; break;
                    default: break;
                }
            }
        }

        private void GetChildNodeAttributes(XmlNode node, string pNodePath , List<PartNodeInfo> listInfos)
        {
            XmlNodeList nodes = node.ChildNodes;
            foreach (XmlNode t_node1 in nodes)
            {
                PartNodeInfo pInfo = new PartNodeInfo();
                pInfo.pNodePath = pNodePath;
                GetSingleNodeAttributes(t_node1, pInfo);
                listInfos.Add(pInfo);
                GetChildNodeAttributes(t_node1, pInfo.nodePath, listInfos);
            }
        }

        /// <summary>
        /// 获取xml文件所有节点
        /// </summary>
        /// <returns></returns>
        public List<PartNodeInfo> GetAllNode()
        {
            List<PartNodeInfo> allNodes = new List<PartNodeInfo>();
            XmlElement rootNode = doc.DocumentElement;
            PartNodeInfo pInfo = new PartNodeInfo();
            pInfo.pNodePath = "";
            GetSingleNodeAttributes(rootNode, pInfo);    
            allNodes.Add(pInfo);
            GetChildNodeAttributes(rootNode, pInfo.nodePath, allNodes);
            return allNodes;
        }

        /// <summary>
        /// 添加新节点到已知节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="parentNodeInfo"></param>
        public void Append2Node(PartNodeInfo node, PartNodeInfo parentNodeInfo)
        {
            XmlNode pNode = doc.SelectSingleNode(parentNodeInfo.nodePath);
            if (pNode != null)
            {
                XmlElement t_element = doc.CreateElement(node.nodeName);
                if (node.name != null)
                {
                    t_element.SetAttribute("name", node.name);
                }
                if (node.ID != null)
                {
                    t_element.SetAttribute("ID", node.ID);
                }
                if (node.stdDesign != null)
                {
                    t_element.SetAttribute("设计规范", node.stdDesign);
                }
                if (node.temDraw != null)
                {
                    t_element.SetAttribute("模板图纸", node.temDraw);
                }
                pNode.AppendChild(t_element);
            }
        }
        /// <summary>
        /// 保存更新xml
        /// </summary>
        /// <param name="xmlPath"></param>
        public void SaveXml(string xmlPath)
        {
            doc.Save(xmlPath);
        }

        /// <summary>
        /// 批量创建节点
        /// 1.先使用CreaetXmlObj获取新创建的xml文件
        /// 2.传入具有树结构的 List PartNodeInfo对象
        /// </summary>
        /// <param name="listInfos"></param>
        public void CreateElements(List<PartNodeInfo> listInfos)
        {
            if (listInfos.Count > 0)
            {
                string rootNode = doc.DocumentElement.Name;
                rootNode = "/" + rootNode;
                PartNodeInfo rootNodeInfo = new PartNodeInfo();
                rootNodeInfo.nodeName = doc.DocumentElement.Name;
                rootNodeInfo.nodePath = rootNode;
                CreateElementNode(doc.DocumentElement, rootNodeInfo, listInfos);
            }
        }
        private void CreateElementNode(XmlElement pElement, PartNodeInfo parentNodeInfo, List<PartNodeInfo> listInfos)
        {
            foreach (PartNodeInfo nodeInfo in listInfos)
            {
                if (nodeInfo.pNodePath == parentNodeInfo.nodePath)
                {
                    Append2Node(nodeInfo, parentNodeInfo);
                    XmlNode t_node = doc.SelectSingleNode(nodeInfo.nodePath);
                    CreateElementNode((XmlElement)t_node, nodeInfo, listInfos);
                }
            }
        }


    }
}
